package basic.study.wantOffer.chapter3;

/**
 * @author Kevin
 * @date 2020/4/23 21:10
 * 单向链表倒数第K个节点
 */
public class Problem22 {

    Node findKthToTail(Node head, int k) {
        // node为空，或者k取值为0
        if (head == null && k == 0) return null;
        Node node = head;
        Node fast = head;
        for (int i = 0; i < k-1; i++) {
            if (fast.next != null) {
                fast = fast.next;
            } else {//如果链表长度小于k
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            node = node.next;
        }

        return node;
    }

    class Node {
        private int data;
        private Node next;
    }
}
